package com.example.aircraftwar.application;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.EditText;

import com.example.aircraftwar.R;
import com.example.aircraftwar.aircraft.BossEnemy;
import com.example.aircraftwar.aircraft.EliteEnemy;
import com.example.aircraftwar.aircraft.HeroAircraft;
import com.example.aircraftwar.aircraft.MobEnemy;
import com.example.aircraftwar.aircraftfactory.BossFactory;
import com.example.aircraftwar.bullet.EnemyBullet;
import com.example.aircraftwar.bullet.HeroBullet;
import com.example.aircraftwar.prop.BloodProp;
import com.example.aircraftwar.prop.BombProp;
import com.example.aircraftwar.prop.BulletProp;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * 综合管理图片的加载，访问
 * 提供图片的静态访问方法
 *
 * @author hitsz
 */
public class ImageManager {

    /**
     * 类名-图片 映射，存储各基类的图片 <br>
     * 可使用 CLASSNAME_IMAGE_MAP.get( obj.getClass().getName() ) 获得 obj 所属基类对应的图片
     */
    private static final Map<String, Bitmap> CLASSNAME_IMAGE_MAP = new HashMap<>();

    public static Bitmap BACKGROUND_IMAGE1;
    public static Bitmap BACKGROUND_IMAGE2;
    public static Bitmap BACKGROUND_IMAGE3;
    public static Bitmap HERO_IMAGE;
    public static Bitmap HERO_BULLET_IMAGE;
    public static Bitmap ENEMY_BULLET_IMAGE;
    public static Bitmap MOB_ENEMY_IMAGE;
    public static Bitmap ELITE_ENEMY_IMAGE;
    public static Bitmap BOSS_ENEMY_IMAGE;
    public static Bitmap BLOOD_PROP_IMAGE;
    public static Bitmap BOMB_PROP_IMAGE;
    public static Bitmap BULLET_PROP_IMAGE;
    public static Bitmap[] EXPLODE_IMAGE = new Bitmap[16];
    private static Field field;
    private static int id;

    public static void initImage(Context context) {
//            BACKGROUND_IMAGE = ImageIO.read(new FileInputStream("src/images/bg.jpg"));
//            BACKGROUND_IMAGE = ImageIO.read(new FileInputStream("src/images/bg2.jpg"));
//            BACKGROUND_IMAGE = ImageIO.read(new FileInputStream("src/images/bg3.jpg"));
//            BACKGROUND_IMAGE = ImageIO.read(new FileInputStream("src/images/bg4.jpg"));
        BACKGROUND_IMAGE1 = BitmapFactory.decodeResource(context.getResources(),R.drawable.bg);
        BACKGROUND_IMAGE2 = BitmapFactory.decodeResource(context.getResources(),R.drawable.bg2);
        BACKGROUND_IMAGE3 = BitmapFactory.decodeResource(context.getResources(),R.drawable.bg5);

        HERO_IMAGE = BitmapFactory.decodeResource(context.getResources(),R.drawable.hero);
        MOB_ENEMY_IMAGE = BitmapFactory.decodeResource(context.getResources(),R.drawable.mob);
        ELITE_ENEMY_IMAGE = BitmapFactory.decodeResource(context.getResources(),R.drawable.elite);
        HERO_BULLET_IMAGE = BitmapFactory.decodeResource(context.getResources(),R.drawable.bullet_hero);
        ENEMY_BULLET_IMAGE = BitmapFactory.decodeResource(context.getResources(),R.drawable.bullet_enemy);
        BOSS_ENEMY_IMAGE = BitmapFactory.decodeResource(context.getResources(),R.drawable.boss);
        BLOOD_PROP_IMAGE = BitmapFactory.decodeResource(context.getResources(),R.drawable.prop_blood);
        BOMB_PROP_IMAGE = BitmapFactory.decodeResource(context.getResources(),R.drawable.prop_bomb);
        BULLET_PROP_IMAGE = BitmapFactory.decodeResource(context.getResources(), R.drawable.prop_bullet);

        for (int i = 0;i < 16;i++){
            try {
                String imgname = "e" + (i + 1);
                id = context.getResources().getIdentifier(imgname, "drawable", context.getPackageName());
                System.out.println(id);
            } catch (Exception e) {
                e.printStackTrace();
            }
            EXPLODE_IMAGE[i] = BitmapFactory.decodeResource(context.getResources(),id);
        }

        CLASSNAME_IMAGE_MAP.put(HeroAircraft.class.getName(), HERO_IMAGE);
        CLASSNAME_IMAGE_MAP.put(MobEnemy.class.getName(), MOB_ENEMY_IMAGE);
        CLASSNAME_IMAGE_MAP.put(EliteEnemy.class.getName(),ELITE_ENEMY_IMAGE);
        CLASSNAME_IMAGE_MAP.put(BossEnemy.class.getName(), BOSS_ENEMY_IMAGE);
        CLASSNAME_IMAGE_MAP.put(HeroBullet.class.getName(), HERO_BULLET_IMAGE);
        CLASSNAME_IMAGE_MAP.put(EnemyBullet.class.getName(), ENEMY_BULLET_IMAGE);
        CLASSNAME_IMAGE_MAP.put(BloodProp.class.getName(), BLOOD_PROP_IMAGE);
        CLASSNAME_IMAGE_MAP.put(BombProp.class.getName(), BOMB_PROP_IMAGE);
        CLASSNAME_IMAGE_MAP.put(BulletProp.class.getName(), BULLET_PROP_IMAGE);
    }

    public static Bitmap get(String className){
        return CLASSNAME_IMAGE_MAP.get(className);
    }

    public static Bitmap get(Object obj){
        if (obj == null){
            return null;
        }
        return get(obj.getClass().getName());
    }

}
